//
//  LPTBaseRequest.m
//  LPTClient
//
//  Created by apple on 2018/12/3.
//  Copyright © 2018 zhaodd. All rights reserved.
//

#import "LPTBaseRequest.h"
#import "NSObject+YYModel.h"


@interface LPTBaseRequest()

@property (nonatomic, assign) YTKRequestMethod m_method;
@property (nonatomic, strong) NSString *m_apiName;
@property (nonatomic, strong) NSDictionary *m_body;
@property (nonatomic, assign) BOOL isFileUpload;

@property (nonatomic) Class m_class;

@end

@implementation LPTBaseRequest

- (void)dealloc
{

}



- (instancetype)initWithApi:(NSString*)apiName withBody:(NSDictionary *)body
{
    self = [super init];
    if (self) {
        self.m_apiName = apiName;
        self.m_body = body;
        self.isFileUpload = NO;
    }
    
    return self;
}

- (instancetype)initWithApi:(NSString*)apiName withBody:(NSDictionary *)body withMethod:(YTKRequestMethod)method;
{
    
    self.m_method = method;
    return [self initWithApi:apiName withBody:body];
    
}

- (void)startRequestCompleteSuccess:(LPTBaseRequestCompletionBlock)success
               completeFailed:(LPTBaseRequestCompletionBlock)failed
{
    self.successBlock = success;
    self.failureBlock = failed;
    self.needLogin = YES;
    
    [super start];
}


- (void)startRequestWithClass:(Class)className
              completeSuccess:(LPTBaseRequestCompletionBlock)success
               completeFailed:(LPTBaseRequestCompletionBlock)failed
{
    self.m_class = className;
    [self startRequestCompleteSuccess:success completeFailed:failed];
}

- (void)startUploadImageCompleteSuccess:(LPTBaseRequestCompletionBlock)success
               completeFailed:(LPTBaseRequestCompletionBlock)failed
{
    self.isFileUpload = YES;
    [self startRequestCompleteSuccess:success completeFailed:failed];
}


- (NSString *)baseUrl
{
    return [NSString stringWithFormat:@"%@%@", BASE_URL, self.m_apiName];
}

- (void)requestCompleteFilter
{
    NSLog(@"\n请求地址：%@ \n请求参数：%@ \n请求结果:%@ \n", self.m_apiName, self.m_body, self.responseJSONObject);
    // 处理请求结果
    if (self.responseObject) {
        NSInteger code = [[self.responseObject objectForKey:@"code"] integerValue];
        self.code = code;
        if (code == 400007) {
            // 登录失效，弹出登录
            [[NSNotificationCenter defaultCenter] postNotificationName:@"auto_present_loginvc" object:nil];
        }
        if (code == 0) {
            NSDictionary *data = [self.responseObject objectForKey:@"data"];
            // 解析
            id obj = [self.m_class modelWithDictionary:data];
            if (!obj) {
                obj = data;
            }
            self.successBlock(self, obj, 0);
        }else{
            self.msg = [self.responseObject objectForKey:@"message"];
            self.failureBlock(self, [self.responseObject objectForKey:@"message"], code);
        }
    }else {
        self.code = -1;
        self.msg = @"请求数据异常";
        self.failureBlock(self, @"请求数据异常", -1);
    }
    [super requestCompleteFilter];
}

- (void)requestFailedFilter
{
    // 处理错误消息， 网络请求系统层错误 -2
    self.code = -2;
    self.msg = @"网络请求异常";
    
    self.failureBlock(self, @"网络请求异常", -2);
    [super requestFailedFilter];
}


/// 在HTTP报头添加的自定义参数
- (NSDictionary *)requestHeaderFieldValueDictionary
{
    if (!self.needLogin) {
        return nil;
    }
    NSString *token = [LTPCacheManager shareInstance].token;
    
    token = [NSString stringWithFormat:@"Bearer %@",token];
    
    NSDictionary *dict = @{
                           @"accept" : self.isFileUpload?@"form-data":@"application/json",
                           @"authorization" : token
                           };
    return dict;
    
}


/// Http请求的方法
- (YTKRequestMethod)requestMethod
{
    return self.m_method;
}


/// 请求的连接超时时间，默认为60秒
- (NSTimeInterval)requestTimeoutInterval
{
    return 15;
}


- (YTKRequestSerializerType)requestSerializerType
{
    return YTKRequestSerializerTypeHTTP;
}

/// 请求的参数列表
- (id)requestArgument
{
    return self.m_body;
}

- (NSInteger)cacheTimeInSeconds
{
    return 3 * 60;
}

- (long long)cacheVersion
{
    return 1000;
}


@end
